<template>
  <div>
    <!--车型、工时单价关系维护 编辑-->
    <div style="padding-top: 1rem;"></div>
    <FormPanel ref="formPanel" :list="formList1" :cols="1" :formType="'add'" labelWidth="110" :isSubmitBtn="false" @formChange="formChangeHandle" />
    <!-- 选择车型弹窗 -->
    <BaseDialog :visible.sync="visibleDrawer" :title="'选择车型'" width="850px" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <Vehicle @close="close" @save="save"></Vehicle>
    </BaseDialog>
    <div style="padding-top: 1rem;"></div>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <multiuse-button type="primary" :click="saveDialog.bind()">保 存</multiuse-button>
      <multiuse-button @click="closeDialog">关 闭</multiuse-button>
    </div>
  </div>
</template>
<script>
import Vehicle from '@/pages/claimDemage/tools/ModelVehicle.vue';
import { editSave,selectById } from '@/api/claim/factory/ModelAndLabour';
import { copyUpdateData } from '@/utils/index';
export default {
props: ['rowData'],
  components: {
    Vehicle
  },
  data() {
    return {
      loading: false,
      visibleDrawer: false,
      visible: false,
      fetchParams: {}, //ipa 接口
      selectedRows: [], //选中经销商
      formList1: this.createFormList1(),
      saveData: null,
      //newVar:null,
      newVar1: {}
    };
  },
  created() {
  },
mounted() {
    console.log(this.rowData,"this.rowDatathis.rowDatathis.rowDatathis.rowData")
    this.getDetailFun(this.rowData.mlId);
  },
  methods: {
    async getDetailFun(id) {
      this.loading = true;
      let res = (await selectById({ mlId: id })).data;
      //this.newVar = id;
      console.log(res, '11111111111111111111111111111111111111111111111111');
      this.newVar1 = res;
      console.log(this.newVar1, 'this.newVar1this.newVar1this.newVar1');
      for (let i in res) {
        this.formList1.map(x => {
          if (x.fieldName == i) {
            x.initialValue = res[i];
          }
        });
      }
      
      this.loading = false;

    },

    formChangeHandle(val) {
      console.log(val);
      this.saveData = val;
    },
    //搜索获取数据
    changeHandle(val) {
      this.fetchParams = val;
      console.log(val);
    },

    //保存
    async saveDialog() {
      this.$refs.formPanel.SUBMIT_FORM();
      console.log(this.saveData,"this.saveDatathis.saveData");
      console.log(this.newVar1,"this.saveDatathis.saveData");
      if (this.saveData !== null) {
        this.loading = true;
        this.newVar1 = copyUpdateData(this.newVar1,this.saveData);
        //this.saveData.mlId = this.newVar;
        console.log(this.newVar1,"this.saveDatathis.saveData");
        console.log(this.saveData,"this.saveDatathis.saveData");
        let res = await editSave(this.newVar1);
        console.log(res,"执行方法");
        if (res.resultCode === 200) {
          this.$emit('save');
          return this.$notify.success({ title: '提示信息', message: '保存成功' });
        } else {
          this.loading = false;
        }
      }
    },
    async save(obj) {
      this.formList1.find(x => x.fieldName == 'modelCode').initialValue = obj[0].modelCode;
      this.modelName = obj[0].modelName;
      this.modelCode = obj[0].modelCode;
    
      this.visibleDrawer = false;
    },
    close() {
      this.visible = false;
    },
    //取消操作
    closeDialog() {
      this.$emit('close', { visible: false, data: this.selectedRows, type: 'close' });
    },
    chooseOrderNo() {
      this.visibleDrawer = true;
    },
    createFormList1() {
      return [
        {
          type: 'INPUT',
          label: '车型',
          fieldName: 'modelCode',
          placeholder: '请选择车型...',
          initialValue: '',
          readonly: true,
          rules: [{ required: true, message: '请选择车型...', trigger: 'change' }],
          unitRender: p => {
            return (
              <div>
                <multiuse-button icon="el-icon-search" onClick={() => this.chooseOrderNo()} />
              </div>
            );
          }
        },
        { type: 'INPUT', label: '索赔工时单价', fieldName: 'cmsClaimPrice', placeholder: '请输入',rules: [{ required: true, message: '请输入工时单价...', trigger: 'change' }], initialValue: '' },
        {
          type: 'SELECT',
          label: '状态',
          filterable: true,
          fieldName: 'status',
          placeholder: '请选择状态...',
          itemList: [
            { text: '启用', value: 1 },
            { text: '禁用', value: 0 }
          ],
          rules: [{ required: true, message: '请选择状态...', trigger: 'change' }]
        }
      ];
    }
  }
};
</script>
<style lang="less" scoped></style>
